﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SQLToolbox.Controller.Tools;

using SQLToolbox.Types;

namespace SQLToolbox.Controller.UnitTests
{
	[TestFixture]
	public class DBConnectorUnitTests
	{
		/// <summary>
		///A test for AvailableDataSources
		///</summary>
		[Test]
		public void AvailableDataSourcesTest()
		{
			DBConnector target = new DBConnector();
			int i = 0;
			foreach (string s in target.AvailableDataSources)
			{
				i++;
			}
			Assert.IsTrue(i > 0);
		}

		/// <summary>
		///A test for TryToConnect
		///</summary>
		[Test]
		public void TryToConnectTest()
		{
			DBConnector target = new DBConnector();
			DBConnectionProperties conProp = new DBConnectionProperties { DataSource = "MS SQL 2005", ServerName = @".\sql2005", Credentials = new ConnectionCredentials { IntegratedSecurity = true }, DatabaseName = "test" };
			bool expected = true;
			bool actual;
			actual = target.TryToConnect(conProp);
			Assert.AreEqual(expected, actual);
			conProp = new DBConnectionProperties { DataSource = "MS SQL 2005", ServerName = @".\sql2005", Credentials = new ConnectionCredentials { Username = "a", Password = "a" }, DatabaseName = "test" };
			expected = false;
			actual = target.TryToConnect(conProp);
			Assert.AreEqual(expected, actual);
		}

		/// <summary>
		///A test for GetServers
		///</summary>
		[Test]
		public void GetServersTest()
		{
			DBConnector target = new DBConnector();
			string dataSourceName = "MS SQL 2005";

			int i = 0;
			foreach (string s in target.GetServers(dataSourceName))
			{
				i++;
			}
			Assert.IsTrue(i > 0);
		}

		/// <summary>
		///A test for GetSchema
		///</summary>
		[Test]
		public void GetSchemaTest()
		{
			DBConnector target = new DBConnector();
			DBConnectionProperties conProp = new DBConnectionProperties{DataSource="MS SQL 2005", ServerName=@".\sql2005",Credentials= new ConnectionCredentials{IntegratedSecurity = true},DatabaseName= "test"};
			Schema actual = target.GetSchema(conProp);

			Assert.IsTrue(actual.Tables.Contains("Table_1"));
			Assert.IsTrue(actual.Views.Contains("View_1"));
			Assert.IsTrue(actual.Procedures.Contains("test_proc"));


		}


		/// <summary>
		///A test for GetSchema
		///</summary>
		[Test]
		public void GetSchemaTestWithTypes()
		{
			DBConnector target = new DBConnector();
			DBConnectionProperties conProp = new DBConnectionProperties { DataSource = "MS SQL 2005", ServerName = @".\sql2005", Credentials = new ConnectionCredentials { IntegratedSecurity = true }, DatabaseName = "test" };
			Dictionary <SchemaObjectType, IEnumerable<string>> dict = new Dictionary<SchemaObjectType,IEnumerable<string>>();
			dict.Add(SchemaObjectType.ForeignKey, new string[] { "ReferencedTable" });
			Schema actual = target.GetSchema(conProp, dict);

			Assert.IsTrue(actual.Tables.Contains("Table_1"));
			Assert.IsTrue(actual.Views.Contains("View_1"));
			Assert.IsTrue(actual.Procedures.Contains("test_proc"));
			Assert.IsTrue(actual.Tables[1].AttachedObjects[0].RequestedProperties.ContainsKey("ReferencedTable"));
			Assert.AreEqual("Table_1",actual.Tables[1].AttachedObjects[0].RequestedProperties["ReferencedTable"]);


		}

		/// <summary>
		///A test for GetDatabases
		///</summary>
		[Test]
		public void GetDatabasesTest()
		{
			DBConnector target = new DBConnector();
			DBConnectionProperties conProp = new DBConnectionProperties { DataSource = "MS SQL 2005", ServerName = @".\sql2005", Credentials = new ConnectionCredentials{IntegratedSecurity = true} };
			bool containsTestDB = false;
			foreach (string s in target.GetDatabases(conProp))
			{
				if (string.Compare(s, "test", true) == 0)
					containsTestDB = true;
			}
			Assert.IsTrue(containsTestDB);
		}
	}
}
